const AJAX = getApp().ajax,
  $wx = getApp().$wx,
  getColor = getApp().globalData.getColor;

Page({
  data: {
    getColor: getColor(),
    orderStatus: {
      10: {
        order_text: '待付款',
        btn_text: '立即支付',
        fun: 'pay',
        status: 1
      },

      20: {
        order_text: '待接单',
        btn_text: '商家待接单',
        fun: 'cancelOrder',
        status: 2
      },

      21: {
        order_text: '已接单',
        btn_text: '商家已接单',
        fun: 'viewOrderDetail',
        status: 2
      },

      30: {
        order_text: '查看详情',
        btn_text: ' 配送中',
        fun: 'viewOrderDetail',
        status: 1
      },

      40: {
        order_text: '已完成',
        btn_text: '立即评价',
        fun: 'rating',
        status: 2
      },

      50: {
        order_text: '已取消',
        btn_text: '删除订单',
        fun: 'delOrder',
        status: 3
      },

      60: {
        order_text: '已退单',
        btn_text: '删除订单',
        fun: 'delOrder',
        status: 3
      }
    },
    isPayment: false,
    paymentType: {
      balanceMoney: 1,
      WeCahtMoney: 2
    }
  },

  onLoad(options) {
    $wx.setNavColor(getColor().val);

    this.getOrderDetail(options.order_id);
    this.getMyMoney();
   ;
  },

  onShareAppMessage() {

  },

  // 打电话
  ringUp(e) {
    let num = e.currentTarget.dataset.num;

    wx.makePhoneCall({
      phoneNumber: num
    });
  },

  // 支付
  pay(e) {
    this.setData({
      isPayment: true,
      orderId: e.currentTarget.dataset.id
    });
  },

  paymentConfirm(e) {
    let _this = this, td = _this.data,
      payment_info = e.detail.payment;

    if (!payment_info.type) {
      $wx.showToast('请选择支付方式');
    } else {
      _this.payOrder({
        pay_type: td.paymentType[payment_info.type],
        order_id: td.orderId
      });
    }
  },

  // 评论
  rating(e) {
    let is_comment = e.currentTarget.dataset.comment,
      order_id = e.currentTarget.dataset.id;

    if (is_comment == 1) {
      wx.navigateTo({
        url: '/pages/other-pages/order-rate/order-rate?order_id=' + order_id
      });
    }
  },

  // 删除订单
  delOrder(e) {
    let _this = this, td = _this.data,
      order_id = e.currentTarget.dataset.id;

    wx.showModal({
      title: '提示',
      content: '确定要删除订单',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '删除中...',
            mask: true
          });

          AJAX.delOrder({
            order_id: order_id
          }, (status, data, msg) => {
            wx.hideLoading();
            if (status) {
              wx.navigateBack();
            } else {
              $wx.showToast(msg);
            }
          });
        }
      }
    });
  },
  

  // 退单
  cancelOrder(e){
    let _this = this, td = _this.data,
        order_id = e.currentTarget.dataset.id;

    wx.showModal({
      title: '提示',
      content: '确定要退单吗',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '退单中...',
            mask: true
          });

          AJAX.cancelOrder({
            order_id: order_id
          }, (status, data, msg) => {
            wx.hideLoading();
            if (status) {
              $wx.showToast(msg, setTimeout(callback => {
                wx.navigateBack();
              }, 2000));
            } else {
              $wx.showToast(msg);
            }
          });
        }
      }
    });
  },
  
  /**
   * 支付
   * @param pay_type
   * @param order_id
   * */
  payOrder(params = {}) {
    let _this = this;
    wx.showLoading({
      title: '支付中...'
    });

    AJAX.payOrder(params, (status, data, msg) => {
      wx.hideLoading();

      if (status) {
        if (params.pay_type == 1) {
          wx.navigateBack({
            success() {
              $wx.showToast('支付成功');
            }
          });
        } else if (params.pay_type == 2) {
          wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            success(res) {
              wx.navigateBack({
                success() {
                  $wx.showToast('支付成功');
                }
              });
            },
            fail(res) {
              $wx.showToast('支付失败');
            }
          });
        }
      } else {
       if(data.code == 202){
         wx.showModal({
           content: msg,
           showCancel: false,
           success(res){
             if(res.confirm){
               wx.navigateBack();
             }
           }
         })
       }else{
         $wx.showToast('支付失败');
       }
      }
    });
  },

  /**
   * 订单详情
   * @param order_id
   * */ 
  getOrderDetail(orderId){
    let _this = this;
    wx.showNavigationBarLoading();

    AJAX.getOrderDetail({
      order_id: orderId
    }, (status, data, msg)=>{
      wx.hideNavigationBarLoading();

      if(status){
        _this.setData({
          orderDetail: data
        });
      }else{
        $wx.showToast(msg);
      }
    });
  },

  /*用户余额*/
  getMyMoney(params = {}) {
    let _this = this;
    AJAX.getMyMoney(params, (status, data, msg) => {
      if (status) {
        _this.setData({
          myMoney: data.money
        });
      } else {
        $wx.showToast(msg);
      }
    });
  }
});